package com.dingwei.superiordiary.adapter.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.app.MyApplication;
import com.dingwei.superiordiary.bean.MyReleaseBean;
import com.dingwei.superiordiary.utils.DialogUtil;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.lihao.baselibrary.base.BaseActivity;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by lihao on 2018/1/8.
 */

public class MyReleaseAdapter extends RecyclerArrayAdapter<MyReleaseBean.DataBean.ListBean> {


    private DeleteMyReleaseInterFace deleteMyReleaseInterFace;

    public void deleteMyReleaseInterFaceListener(DeleteMyReleaseInterFace listener){
        deleteMyReleaseInterFace = listener;

    }


    public MyReleaseAdapter(Context context) {
        super(context);
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyReleaseHolder(parent);
    }
    class MyReleaseHolder extends BaseViewHolder<MyReleaseBean.DataBean.ListBean> {


        @Bind(R.id.tv_day)
        TextView tvDay;
        @Bind(R.id.img_delete)
        ImageView imgDelete;
        @Bind(R.id.img)
        ImageView img;
        @Bind(R.id.tv_content)
        TextView tvContent;
        @Bind(R.id.tv_look)
        TextView tvLook;
        @Bind(R.id.tv_like)
        TextView tvLike;
        @Bind(R.id.tv_collect)
        TextView tvCollect;
        @Bind(R.id.tv_month)
        TextView tvMonth;

        public MyReleaseHolder(ViewGroup parent) {
            super(parent, R.layout.item_my_release);
            ButterKnife.bind(this, itemView);
        }

        @Override
        public void setData(MyReleaseBean.DataBean.ListBean data) {
            super.setData(data);
            tvDay.setText(data.addtime.day);
            tvMonth.setText(data.addtime.month);
            Glide.with(BaseActivity.context)
                    .load(data.image)
                    .apply(MyApplication.options)
                    .into(img);
            tvContent.setText(data.title);

            tvLook.setText(data.viewed);
            tvLike.setText(data.likes);
            tvCollect.setText(data.collection);

            imgDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    DialogUtil.showAlertDialogChoose(BaseActivity.context, "温馨提示", "删除内容后其中的所有内容都会被删除", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            switch (which) {
                                case DialogUtil.BUTTON1:
                                    dialog.dismiss();

                                    break;
                                case DialogUtil.BUTTON2:
                                    dialog.dismiss();
                                    if (deleteMyReleaseInterFace!=null){
                                        deleteMyReleaseInterFace.deleteMyRelease(getDataPosition());
                                    }
                                    break;
                            }
                        }
                    });

                }
            });

        }
    }


    public interface DeleteMyReleaseInterFace {

        /**
         * 删除我的发布
         * @param position
         */
        void  deleteMyRelease(int position);
    }
}
